<?php

/**
 * 违规合同控制器
 * @author yuzy 2015-5-13
 */

namespace mybackend\controllers;

use Yii;
use common\models\myhtsq\ViolatedContractModel;
use common\services\myhtsq\ViolatedContractService;
use common\entities\myhtsq\ActionCode;

class ViolatedContractController extends BaseController
{

    protected $_contractService;

    public function __construct($id, $module, ViolatedContractService $contractService, $config = [])
    {
        $this->_contractService = $contractService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules()
    {
        return [
            'actions' => ['GetViolatedContractList', 'GetViolatedContractInfo', 'SaveViolatedContract', 'ExportViolatedContractList'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'GetViolatedContractList' => ActionCode::VIOLATED_CONTRACT_VIEW,
                        'GetViolatedContractInfo' => ActionCode::VIOLATED_CONTRACT_VIEW,
                        'SaveViolatedContract' => ActionCode::VIOLATED_CONTRACT_MNG,
                        'ExportViolatedContractList' => ActionCode::VIOLATED_CONTRACT_MNG
                    ]
                ]
            ]
        ];
    }

    /**
     * 违规合同列表
     * @param integer $page 页码
     * @param integer $pageSize 页条数
     * @param string $sortBy 排序字段
     * @param boolean $sortAsc 是否升序
     * @param integer $areaId 区域id
     * @param string $keyword 关键字
     * @return array
     */
    public function actionGetViolatedContractList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId)
    {
        $result = $this->_contractService->getViolatedContractList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId);
        $this->exportJson($result, $this->_contractService->getErrCode(), $this->_contractService->getErrMsg());
    }

    /**
     * 获取违规合同信息
     * @param string $contractId
     * @return \common\models\myhtsq\ViolatedContractModel
     */
    public function actionGetViolatedContractInfo($contractId)
    {
        $result = $this->_contractService->getViolatedContractInfo($contractId);
        $this->exportJson($result, $this->_contractService->getErrCode(), $this->_contractService->getErrMsg());
    }

    /**
     * 保存违规合同
     */
    public function actionSaveViolatedContract()
    {
        $this->exitIfNotPost();
        $model = new ViolatedContractModel();
        if ($model->load($_POST['item'], '')) {
            $attachmentList = [];
            if (isset($_POST['attachmentList'])) {
                $attachmentList = $_POST['attachmentList'];
            }
            if ($model->violated_contract_id == '') {
                $result = $this->_contractService->addViolatedContract($model, $attachmentList);
            } else {
                $result = $this->_contractService->updateViolatedContract($model, $attachmentList);
            }

            $this->exportJson($result, $this->_contractService->getErrCode(), $this->_contractService->getErrMsg());
        } else {
            $this->errorParam();
        }
    }

    /**
     * 导出违规合同
     * @param type $keyword 关键字
     * @param type $areaId 区域
     */
    public function actionExportViolatedContractList()
    {
        $keyword = $this->getParam('keyword', 'post');
        $areaId = $this->getParam('areaId', 'post');
        $data = $this->_contractService->getViolatedContractList("", "", "created_on", "", $keyword, $areaId);
        $this->_contractService->exportViolatedContractList($data['items']);
        $this->exportJson($data, $this->_contractService->getErrCode(), $this->_contractService->getErrMsg());
    }


}
